package com.zzj.seckill.controller;

import com.zzj.seckill.mapper.UserMapper;
import com.zzj.seckill.pojo.User;
import com.zzj.seckill.service.IGoodsService;
import com.zzj.seckill.service.IUserService;
import com.zzj.seckill.vo.DetailVo;
import com.zzj.seckill.vo.GoodsVo;
import com.zzj.seckill.vo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;
    /**
     * 优化前：1175
     * @param model
     * @param user
     * @return
     */
    @RequestMapping(value = "/toList",produces="text/html;charset=utf-8")
    @ResponseBody
    public String toList(Model model,User user,HttpServletRequest request,HttpServletResponse httpServletResponse){
//        if(StringUtils.isEmpty(ticket)){
//            return "login";
//        }
//        User user = (User)session.getAttribute(ticket);
//        User user = userService.getUserByCookie(ticket, request, response);
//        if(user == null){
//            return "login";
//        }
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String html = ((String) valueOperations.get("goodsList"));
        if(!StringUtils.isEmpty(html)){
            return html;
        }
        model.addAttribute("user",user);
        model.addAttribute("goodsList",goodsService.findGoodsVo());
        WebContext webContext = new WebContext(request,httpServletResponse,request.getServletContext(),request.getLocale(),model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("goodsList",webContext);
        if(!StringUtils.isEmpty(html)){
            valueOperations.set("goodsList",html,60, TimeUnit.SECONDS);
        }
        return html;
    }
    @RequestMapping(value = "/detail/{goodsId}")
    @ResponseBody
    public RespBean toDetail(Model model, User user, @PathVariable Long goodsId, HttpServletRequest request, HttpServletResponse httpServletResponse){
//        ValueOperations valueOperations = redisTemplate.opsForValue();
//        String str = (String) valueOperations.get("goodsDetails" + goodsId);
//        if(!StringUtils.isEmpty(str)){
//            return str;
//        }
//        model.addAttribute("user",user);
        GoodsVo goodsVoByGoodsId = goodsService.findGoodsVoByGoodsId(goodsId);
//        System.out.println(goodsVoByGoodsId);
        Date startDate = goodsVoByGoodsId.getStartDate();
        Date endDate = goodsVoByGoodsId.getEndDate();
        Date date = new Date();
        int secKillStatus = 0;
        int remainSeconds = 0;
        if(date.before(startDate)){
            remainSeconds = ((int) ((startDate.getTime() - date.getTime()) / 1000));
            System.out.println(date+"==="+startDate+"==="+endDate);
        }else if(date.after(endDate)){
            secKillStatus = 2;
            remainSeconds = -1;
        }else{
            secKillStatus = 1;
            remainSeconds = 0;
        }
//        model.addAttribute("goods",goodsVoByGoodsId);
//        model.addAttribute("secKillStatus",secKillStatus);
//        model.addAttribute("remainSeconds",remainSeconds);
//        WebContext webContext = new WebContext(request, httpServletResponse, request.getServletContext(), request.getLocale(), model.asMap());
//        str = thymeleafViewResolver.getTemplateEngine().process("goodsDetail",webContext);
//        if(!StringUtils.isEmpty(str)){
//            valueOperations.set("goodsDetail"+goodsId,str,60,TimeUnit.SECONDS);
//        }
//        return str;
        DetailVo detailVo = new DetailVo();
        detailVo.setUser(user);
        detailVo.setGoodsVo(goodsVoByGoodsId);
        detailVo.setRemainSeconds(remainSeconds);
        detailVo.setSecKillStatus(secKillStatus);
        System.out.println(detailVo);
        return RespBean.success(detailVo);
    }
}
